package sjtu.ist.warehouse4ws.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import sjtu.ist.warehouse4ws.dao.ModelgroupDaoHibernate;
import sjtu.ist.warehouse4ws.dao.cust.CustModelgroupDaoHibernate;
import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.model.CustModelgroup;
import sjtu.ist.warehouse4ws.model.Modelgroup;
import sjtu.ist.warehouse4ws.tool.Constants;
import sjtu.ist.warehouse4ws.tool.MyUtils;
import sjtu.ist.warehouse4ws.tool.PropertyHelper;
import sjtu.ist.warehouse4ws.tool.file.ModelPackage;
import sjtu.ist.warehouse4ws.vo.ModelgroupVO;

public class ModelgroupServiceImpl implements ModelgroupService{

	private ModelgroupDaoHibernate modelgroupDao;
	private CustModelgroupDaoHibernate custModelgroupDao;
	private RepositoryMoudl repositoryMoudlService;
	
	@Override
	public Modelgroup get(Integer id) {
		return modelgroupDao.get(id);
	}
	
	@Override
	public Integer save(Modelgroup modelgroup, int companyId) {
		// TODO Auto-generated method stub
		Integer groupId=0;
		if(companyId==Constants.ADMIN_COMPANY_ID){
			groupId=modelgroupDao.save(modelgroup);
		}else{
			CustModelgroup cm=new CustModelgroup();
			PropertyHelper.copyProperties(modelgroup, cm);
			groupId=custModelgroupDao.save(cm);
		}
		return groupId;
	}
	
	@Override
	public void update(Modelgroup modelgroup,int companyId) {
		if(companyId==Constants.ADMIN_COMPANY_ID){
			modelgroupDao.update(modelgroup);
		}else{
			CustModelgroup cm=new CustModelgroup();
			PropertyHelper.copyProperties(modelgroup, cm);
			custModelgroupDao.update(cm);
		}
		
	}

	@Override
	public void delete(Integer id) {
		modelgroupDao.delete(id);
	}

	@Override
	public void delete(Modelgroup modelgroup) {
		modelgroupDao.delete(modelgroup);
	}

	@Override
	public List<ModelgroupVO> getAll() {
		// TODO Auto-generated method stub
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin=currentUser.hasRole("superadmin");
		List<ModelgroupVO> list=new ArrayList<ModelgroupVO>();
		if(admin){
			List<Modelgroup> mlist = modelgroupDao.getAll();
			for(Modelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}else{
			List<CustModelgroup> mlist = custModelgroupDao.getAll();
			for(CustModelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}
		
		return list;		
	}
	
	@Override
	public List<ModelgroupVO> getDistinctName() {
		// TODO Auto-generated method stub
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin=currentUser.hasRole("superadmin");
		List<ModelgroupVO> list=new ArrayList<ModelgroupVO>();
		if(admin){
			List<Modelgroup> mlist = modelgroupDao.getDistinctName();
			for(Modelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}else{
			List<CustModelgroup> mlist = custModelgroupDao.getDistinctName();
			for(CustModelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}
		
		return list;
		
	}
	
	@Override
	public List<ModelgroupVO> getAllByName(String name) {
		// TODO Auto-generated method stub
		Subject currentUser = SecurityUtils.getSubject();
		boolean admin=currentUser.hasRole("superadmin");
		List<ModelgroupVO> list=new ArrayList<ModelgroupVO>();
		if(admin){
			List<Modelgroup> mlist = modelgroupDao.findByName(name);
			for(Modelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}else{
			List<CustModelgroup> mlist = custModelgroupDao.findByName(name);
			for(CustModelgroup m : mlist){
				ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
				list.add(vo);
			}
		}
		
		return list;
		
	}
	

	@Override
	public List<Modelgroup> getByName(String name) {
		// TODO Auto-generated method stub
		return (List<Modelgroup>) modelgroupDao.findByName(name);
	}

	@Override
	public List<ModelgroupVO> getAllByAdmin() {
		// TODO Auto-generated method stub		
		List<ModelgroupVO> list=new ArrayList<ModelgroupVO>();
		List<Modelgroup> mlist = modelgroupDao.getAll();
		for(Modelgroup m : mlist){
			ModelgroupVO vo=new ModelgroupVO(m.getId(),m.getGroupName(),m.getDescription(),0,m.getVersion());
			list.add(vo);
		}
		
		return list;
	}
	
	public void setCustModelgroupDao(CustModelgroupDaoHibernate custModelgroupDao){
		this.custModelgroupDao = custModelgroupDao;
	}
	
	public void setModelgroupDao(ModelgroupDaoHibernate modelgroupDao) {
		this.modelgroupDao = modelgroupDao;
	}

	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
		this.repositoryMoudlService = repositoryMoudlService;
	}

	
			
}
